package com.ruoyi.web.controller.system;


import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
import com.ruoyi.system.domain.GwType;
import com.ruoyi.system.service.GwIService;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.system.service.impl.SysDictTypeServiceImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("gwType")
public class GwTypeController {


    @Resource
    private GwIService gwIService;

    @Resource
    private ISysDictTypeService sysDictTypeService;


    @Anonymous
    @GetMapping("test")
    @DataSource(DataSourceType.SLAVE)
    public boolean test(){
        return list(gwIService.list());
    }


    public boolean list(List<GwType> gwTypeList){

        boolean flag=false;
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());


        for (GwType gwType : gwTypeList) {

            SysDictType sysDictType=new SysDictType();
            sysDictType.setRemark(gwType.getId());
            sysDictType.setDictName(gwType.getName());
            sysDictType.setDictType(gwType.getType());
            sysDictType.setStatus("0");
            sysDictType.setCreateBy("admin");
            sysDictType.setCreateTime(new Date());

            flag=sysDictTypeService.save(sysDictType);
        }

        DynamicDataSourceContextHolder.clearDataSourceType();

        return flag;


    }

}
